package jeesample.dtos;

import java.security.Principal;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "EchoDTO")
@XmlAccessorType(XmlAccessType.FIELD)
public class EchoDTO {
	private String content;

	private String serverInfo;
	private String clientInfo;
	private String requestPathInfo;
	private String requestMethod;

	private String headerXRequestedWith;
	private String headerAccept;
	private String headerContentType;
	private String headerAcceptLanguage;
	private String headerUserAgent;
	private String uriRawPath;
	private String uriRawQuery;
	private String authenticationScheme;
	private String principalName;

	public EchoDTO() {
	}

	public EchoDTO(String msg) {
		content = msg;
	}

	public void fill(HttpServletRequest request) {
		serverInfo = request.getLocalAddr() + ":" + request.getLocalPort();
		clientInfo = request.getRemoteAddr() + ":" + request.getRemotePort();
		requestPathInfo = request.getPathInfo();
		requestMethod = request.getMethod();
		headerXRequestedWith = request.getHeader("X-Requested-With");
		headerAccept = request.getHeader("Accept");
		headerContentType = request.getHeader("Content-Type");
		headerAcceptLanguage = request.getHeader("Accept-Language");
		headerUserAgent = request.getHeader("User-Agent");
	}

	public void fill(UriInfo uriInfo) {
		uriRawPath = uriInfo.getAbsolutePath().getRawPath();
		uriRawQuery = uriInfo.getAbsolutePath().getRawQuery();
	}

	public void fill(SecurityContext securityContext) {
		authenticationScheme = securityContext.getAuthenticationScheme();
		Principal principal = securityContext.getUserPrincipal();
		if (principal != null) {
			principalName = principal.getName();
		}
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getServerInfo() {
		return serverInfo;
	}

	public void setServerInfo(String serverInfo) {
		this.serverInfo = serverInfo;
	}

	public String getClientInfo() {
		return clientInfo;
	}

	public void setClientInfo(String clientInfo) {
		this.clientInfo = clientInfo;
	}

	public String getRequestPathInfo() {
		return requestPathInfo;
	}

	public void setRequestPathInfo(String requestPathInfo) {
		this.requestPathInfo = requestPathInfo;
	}

	public String getRequestMethod() {
		return requestMethod;
	}

	public void setRequestMethod(String requestMethod) {
		this.requestMethod = requestMethod;
	}

	public String getHeaderXRequestedWith() {
		return headerXRequestedWith;
	}

	public void setHeaderXRequestedWith(String headerXRequestedWith) {
		this.headerXRequestedWith = headerXRequestedWith;
	}

	public String getHeaderAccept() {
		return headerAccept;
	}

	public void setHeaderAccept(String headerAccept) {
		this.headerAccept = headerAccept;
	}

	public String getHeaderContentType() {
		return headerContentType;
	}

	public void setHeaderContentType(String headerContentType) {
		this.headerContentType = headerContentType;
	}

	public String getHeaderAcceptLanguage() {
		return headerAcceptLanguage;
	}

	public void setHeaderAcceptLanguage(String headerAcceptLanguage) {
		this.headerAcceptLanguage = headerAcceptLanguage;
	}

	public String getHeaderUserAgent() {
		return headerUserAgent;
	}

	public void setHeaderUserAgent(String headerUserAgent) {
		this.headerUserAgent = headerUserAgent;
	}

	public String getUriRawPath() {
		return uriRawPath;
	}

	public void setUriRawPath(String uriRawPath) {
		this.uriRawPath = uriRawPath;
	}

	public String getUriRawQuery() {
		return uriRawQuery;
	}

	public void setUriRawQuery(String uriRawQuery) {
		this.uriRawQuery = uriRawQuery;
	}

}